<template>
  <el-dialog
    :visible.sync="dialogOpen"
    :title="dialogTitle"
    :before-close="cancel"
    width="50%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="数据集名称" prop="setName">
            <el-input v-model="formData.setName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据集类型" prop="pid">
            <el-input v-model="pidName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据量" prop="setCount">
            <el-input v-model="formData.setCount" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据集说明" prop="setDesc">
            <el-input
              v-model="formData.setDesc"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="应用模型" prop="modelId">
            <el-select
              v-model="formData.modelId"
              placeholder="请选择"
              @change="handleChangeModel"
            >
              <el-option
                v-for="(item, index) in modelDatas"
                :key="index"
                :label="item.modelName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="模型版本" prop="versionId">
            <el-select
              v-model="formData.versionId"
              placeholder="请选择"
              @change="handleChangeType"
            >
              <el-option
                v-for="(item, index) in versionDatas"
                :key="index"
                :label="item.versionNo"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="文件上传" prop="files">
            <el-upload
              class="upload"
              ref="upload"
              name="files"
              :action="upload.url"
              accept=".txt, .xls, .xlsx"
              :before-upload="beforeUpload"
              :on-change="handleChange"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
              multiple
              :limit="5"
              :file-list="formData.files"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                请上传1G以内的压缩文件！
              </div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
<script>
import { listAiSets, listVersions, listAiModels, addAiSet, updateAiSet } from "@/api/aifactory";
import { getDict } from "@/api/service";

export default {
  name: "Edit",
  props: [
    "dialogTitle", //标题
    "dialogOpen", //显示
    "dialogData",
  ],
  data() {
    return {
      formData: {},
      pidName: undefined,
      modelDatas: [],
      versionDatas: [],
      upload: {
        url: process.env.VUE_APP_BASE_API + "/file/batch/upload",
      },
      // 表单校验
      rules: {
        setName: [
          { required: true, message: "数据集名称不能为空", trigger: "blur" },
        ],
        setDesc: [
          { required: true, message: "数据集说明不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  created() {},
  mounted() {
    listAiSets().then((res) => {
      if (res.code === 200) {
        res.data.forEach((item) => {
          if (item.id === this.dialogData.pid) {
            this.pidName = item.label;
            this.formData.pid = this.dialogData.pid;
          }
        });
      }
      console.log(this.formData);
    });
    // getDict('point_source').then(res => {
    //   console.log('point_source', res)
    // })
    // getDict('channel_type').then(res => {
    //   console.log('channel_type', res)
    // })

    listAiModels().then((res) => {
      console.log("listAiModels", res);
      if (res.code == 200) {
        this.modelDatas = res.data;
      }
    });
    this.init()
  },
  methods: {
    init() {
      this.formData = this.dialogData;
      console.log(119, "formdata:", this.formData);
    },

    handleChangeModel(row) {
      console.log(169, row);
      listVersions(row).then((res) => {
        console.log("listVersions", res);
        if (res.code == 200) {
          this.versionDatas = res.data;
        }
      });
    },
    handleChangeType(val) {
      console.log(134, val);
      for (let i in this.sourceTypeDatas) {
        if (this.sourceTypeDatas[i].dictLabel == val) {
          this.sourceConfig = this.sourceTypeDatas[i].sourceConfig;
        }
      }
      console.log("sourceConfig:", this.sourceConfig);
      if (val != "file") {
        this.isMysql = true;
      }
      console.log("ismysql:", this.isMysql);
    },
    // 取消按钮
    cancel() {
      this.$emit("closeDialog");
    },
    // 表单重置
    reset() {
      this.formData = {
        sourceName: undefined,
        sourceCode: undefined,
        sourceType: undefined,
        sourceDesc: undefined,
      };
      this.resetForm("form");
    },
    beforeUpload(file) {
      console.log("beforeUpload", file);
      const isExt =
        file.type === "application/x-zip-compressed" ||
        file.name.split(".")[1] === "rar";
      const isLt1G = file.size / 1024 / 1024 / 1024 < 1;

      if (!isExt) {
        this.$message.error("请上传压缩文件！");
      }
      if (!isLt1G) {
        this.$message.error("上传的文件不能超过 1GB!");
      }
      return isExt;
    },
    handleChange(file) {
      // console.log('handleChange', file)
    },
    handleSuccess(res) {
      console.log("handleSuccess", res, this.formData);
      if (res.code == 200) {
        let files = [];
        if (
          this.formData?.setFiles &&
          JSON.parse(this.formData.setFiles).length > 0
        ) {
          files = JSON.parse(this.formData.setFiles);
        }
        console.log(191, files);
        files.push(res.data[0]);
        console.log("193, files", files);
        this.formData.setFiles = JSON.stringify(files);
        console.log("setFiles", this.formData.setFiles);
      }
    },
    handleRemove(file) {
      console.log("handleRemove", file, this.formData);
      this.formData.files.forEach((item, index) => {
        if (file.uid === item.uid) {
          this.formData.files.splice(index, 1);
        }
      });
      this.formData.setFiles = JSON.stringify(this.formData.files);
      console.log("handleRemove22", this.formData);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        console.log("submitForm", this.formData);
        if (this.formData.sourceType != "file") {
          this.formData.sourceConfig = JSON.stringify(this.sourceConfig);
        } else {
          this.formData.sourceConfig = "{}";
        }
        console.log("new submitForm", this.formData);
        if (valid) {
          if (this.formData.id != undefined) {
            console.log(111);
            updateAiSet(this.formData).then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.msg, type: "error" });
              }
            });
          } else {
            addAiSet(this.formData).then((response) => {
              console.log(1111, response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          }
        }
      });
    },
  },
};
</script>
